package router

import (
	"fmt"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"time"
)

type Router struct {
	Id         string     `json:"_id" bson:"_id"` //路由权限标识
	Name       string     `json:"name" bson:"name"`
	Project    string     `json:"project" bson:"project"` //项目名
	Path       string     `json:"path" bson:"path"`
	Method     string     `json:"method" bson:"method"`
	SignMethod string     `json:"sign_method" bson:"signMethod"` //签名方式，无则不签名
	Enable     bool       `json:"enable" bson:"enable"`          // 是否启用
	NeedLogin  bool       `json:"login" bson:"login"`            //是否需要登录
	NeedAuth   bool       `json:"auth" bson:"auth"`              //是否鉴权
	SignType   string     `json:"signType" bson:"signType"`      //是否需要携带签名
	LogConfig  *LogConfig `json:"logConfig" bson:"logConfig"`    //日志配置
	ReqDec     bool       `bson:"reqDec"`                        //请求是否需要解密
	RespEnc    bool       `bson:"respDec"`                       //响应是否需要加密
}

func (r Router) Login() bool {
	return r.NeedLogin
}

func (r Router) Auth() bool {
	return r.NeedAuth
}

func (r Router) AuthAlias() string {
	return r.Id
}

type LogConfig struct {
	ReqMask  []string `json:"reqMask" bson:"reqMask"`   //请求脱敏
	ResMask  []string `json:"resMask" bson:"resMask"`   //响应脱敏
	InDb     bool     `json:"inDb" bson:"inDb"`         //日志是否入库
	ReqPrint bool     `json:"reqPrint" bson:"reqPrint"` //入参是否打印
	ResPrint bool     `json:"resPrint" bson:"resPrint"` //响应是否打印
}

func (r Router) CollectionName() string {
	return "router"
}

func (r Router) GetIndex() []mongo.IndexModel {
	return []mongo.IndexModel{
		{
			Keys:    bson.D{{"path", 1}, {"project", 1}, {"method", 1}},
			Options: options.Index().SetUnique(true),
		},
	}
}

type RequestLog struct {
	Id       primitive.ObjectID `bson:"_id,omitempty"`
	TraceId  string             `bson:"traceId,omitempty"`
	Node     string             `bson:"node,omitempty"`
	Project  string             `bson:"project,omitempty"`
	Method   string             `bson:"method,omitempty"`
	Url      string             `bson:"url,omitempty"`
	ClientId string             `bson:"clientId,omitempty"`
	ClientIp string             `bson:"clientIp,omitempty"`
	Uid      string             `bson:"uid,omitempty"`
	ReqTime  *time.Time         `bson:"reqTime"`
	ReqInfo  *HttpMessageInfo   `bson:"reqInfo"`
	RespTime *time.Time         `bson:"respTime"`
	RespInfo *HttpMessageInfo   `bson:"respInfo"`
}

func (s RequestLog) CollectionName() string {
	return "request_log"
}

func (s RequestLog) GetIndex() []mongo.IndexModel {
	return []mongo.IndexModel{
		{Keys: bson.D{{"url", 1}, {"method", 1}}},
		{Keys: bson.D{{"uid", 1}}},
		{Keys: bson.D{{"clientId", 1}}},
	}
}

type HttpMessageInfo struct {
	Header  map[string][]string `bson:"header,omitempty"`  //http头
	Payload string              `bson:"payload,omitempty"` //http载荷
}

func (s RequestLog) ToString() string {
	str := fmt.Sprintf("uid:%s ,url:%s", s.Uid, s.Url)
	requestBody := ""
	responseBody := ""
	if s.ReqInfo != nil {
		requestBody = fmt.Sprintf(" ,request body:%s", s.ReqInfo.Payload)
	}
	if s.RespInfo != nil {
		responseBody = fmt.Sprintf(" ,response body:%s", s.RespInfo.Payload)
	}
	return str + requestBody + responseBody
}
